# -*- coding: utf-8 -*-
from urllib2 import urlopen, URLError, HTTPError
import zipfile
#from io import StringIO
import csv
import os
from db_conn import *
import re
import traceback
import time
import threading
import Queue
import logging
import datetime

import socket

timeout = 600
socket.setdefaulttimeout(timeout)

def timeStamped(fname, fmt='{fname}_%Y-%m-%d-%H-%M-%S.out'):
    return datetime.datetime.now().strftime(fmt).format(fname=fname)
   
try:      

    WORKERS = 5
    
    class Worker(threading.Thread):
    
        def __init__(self, queue):
            self.__queue = queue
            threading.Thread.__init__(self)
    
        def run(self):
            while 1:
                item = self.__queue.get()
                if item[0] is None:
                    break # reached end of queue
                
                #url = item[0]
                filename = item[1]
                logger = item[2]
                
                try:
                    start = time.time()  
                    #print "URL: %s" % row[-1]
                    
                    #dlfile(url,filename,logger)
                    #print "Tamanho: %s" % url.read().__sizeof__()
                            
                    z = zipfile.ZipFile(filename)
                    #z = zipfile.ZipInfo(filename)
                     
                    #lines_zip = 0
                    try:
                        cursor,conn = db_connect()
                        for line in z.open(z.namelist()[0]).readlines():
                            #print line
                            #logger.debug(line)
                            if line.find("Distancia")==-1:
                            #if lines_zip>0:
                                insert_values = ""
                                for col in line.split("|"):
                                    insert_values += """'%s',""" % (re.escape(col))              
                                
                                insert_anatel="""
                                insert into anatel_real
                                values(NULL,%s'%s',NULL);
                                """ % (insert_values,filename)
                                #print insert_anatel
                                logger.debug(insert_anatel)
                                cursor.execute(insert_anatel)
                            #lines_zip += 1
                        db_disconnect(cursor,conn)
                    except:
                        logger.error(str(sys.exc_info()))
            
                        
                except IOError, e:
                    #print "Nao foi possivel baixar o zip!"
                    #print e
                    logger.error(e)
                except zipfile.error, e:
                    #print "Erro ao descompactar, nao eh zip!"
                    #print e
                    logger.error(e)
                
                task_message = "task ", item[1], " finished! (Tempo Gasto: %s)" % ((time.time() - start))
                print task_message
                logger.info(task_message)
    
    #######LOGGING###################
    LOG_FILENAME = timeStamped('log')
    # create logger
    logger = logging.getLogger("Laguardia")
    logger.setLevel(logging.INFO)
    # create console handler and set level to debug
    ch = logging.StreamHandler()
    #ch = logger.handlers.RotatingFileHandler(
    #          LOG_FILENAME)
    ch.setLevel(logging.DEBUG)
    # create formatter
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # add formatter to ch
    ch.setFormatter(formatter)
    # add ch to logger
    logger.addHandler(ch)
    
    # "application" code
    #logger.debug("debug message")
    #logger.info("info message")
    #logger.warn("warn message")
    #logger.error("error message")
    #logger.critical("critical message")

    queue = Queue.Queue(0)
    
    for i in range(WORKERS):
        Worker(queue).start() # start a worker
    
    ifile  = open('download.csv', "rb")
    reader = csv.reader(ifile)

    for row in reader:
        filename = row[0]+'_'+row[1]+'_'+row[2]+'-'+row[3]+'.zip'
        logger.info(filename)
        url=row[-1]
        queue.put([url,filename,logger])
    
    for i in range(WORKERS):
        queue.put([None,None,None]) # add end-of-queue markers
    
    ifile.close()
    
except KeyboardInterrupt:
    print('Interrompendo!!')
    logger.critical('Interrompendo!!')
    sys.exit()
except:
    exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
    traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                      limit=2, file=sys.stdout)
    logger.error(str(exceptionValue))